#!/bin/bash

# $Id: func_wrt_Makefile 3813 2014-12-09 20:58:02Z pierre $

#=== MAKEFILE HEADER ===

#----------------------------------#
wrt_Makefile_header() {            #
#----------------------------------#
(
    cat << EOF
# This file is automatically generated by jhalfs
# DO NOT EDIT THIS FILE MANUALLY
#
# Generated on `date "+%F %X %Z"`


SHELL = /bin/bash

SRC            = /sources
MOUNT_PT       = $BUILDDIR
PKG_LST        = $PKG_LST
LUSER          = $LUSER
LGROUP         = $LGROUP
LHOME          = $LHOME
SCRIPT_ROOT    = $SCRIPT_ROOT

BASEDIR        = \$(MOUNT_PT)
SRCSDIR        = \$(BASEDIR)/sources
CMDSDIR        = \$(BASEDIR)/\$(SCRIPT_ROOT)/$PROGNAME-commands
LOGDIR         = \$(BASEDIR)/\$(SCRIPT_ROOT)/$LOGDIRBASE
TESTLOGDIR     = \$(BASEDIR)/\$(SCRIPT_ROOT)/$TESTLOGDIRBASE

crCMDSDIR      = /\$(SCRIPT_ROOT)/$PROGNAME-commands
crLOGDIR       = /\$(SCRIPT_ROOT)/$LOGDIRBASE
crTESTLOGDIR   = /\$(SCRIPT_ROOT)/$TESTLOGDIRBASE
crFILELOGDIR   = /\$(SCRIPT_ROOT)/$FILELOGDIRBASE

SU_LUSER       = sudo -u \$(LUSER) -i sh -c
LUSER_HOME     = \$(LHOME)/\$(LUSER)
PRT_DU         = echo -e "\nKB: \`du -skx --exclude=\$(SCRIPT_ROOT) --exclude=lost+found \$(MOUNT_PT) \`\n"
PRT_DU_CR      = echo -e "\nKB: \`du -skx --exclude=\$(SCRIPT_ROOT) --exclude=lost+found / \`\n"

ADD_REPORT       = $REPORT
ADD_CUSTOM_TOOLS = $CUSTOM_TOOLS
ADD_BLFS_TOOLS   = $BLFS_TOOL
PKGMNGT          = $PKGMNGT


export PATH := \${PATH}:/usr/sbin

include makefile-functions

EOF
) > $MKFILE
}

#=======================


#==== PACKAGES NAMES AND VERSIONS ====

#----------------------------------#
get_package_tarball_name() {       #
#----------------------------------#
  local script_name=`echo ${1} | sed -e 's@^[0-9]\{1\}-@@'`

   # The use of 'head' is necessary to limit the return value to the FIRST match..
   # hopefully this will not cause problems.
   #
   case $script_name in
     tcl) echo $(grep "^tcl" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     iputils) echo $(grep "^iputils" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     powerpc-utils) echo $(grep "^powerpc-utils" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     uClibc)
         if [[ "${PROGNAME}" = "clfs3" ]]; then
           echo $(grep "^uClibc-" $JHALFSDIR/pkg_tarball_list | head -n1 )
         else
           echo $(grep "^$script_name-[[:digit:]]" $JHALFSDIR/pkg_tarball_list | head -n1 )
         fi
         ;;
     linux-headers)
         if [[ "${PROGNAME}" = "lfs" ]] || [[ "${PROGNAME}" = "hlfs" ]] || [[ "${PROGNAME}" = "clfs" ]]; then
           # Uses kernel headers directly
           echo $(grep "^linux-[[:digit:]]" $JHALFSDIR/pkg_tarball_list | head -n1 )
         else
           # CLFS{2,3} used to use massaged headers package
           # echo $(grep "^linux-headers-[[:digit:]]" $JHALFSDIR/pkg_tarball_list | head -n1 )
           echo $(grep "^linux-[[:digit:]]" $JHALFSDIR/pkg_tarball_list | head -n1 )
         fi
         ;;
     expect) echo $(grep "^expect" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     e2fsprogs-libs) echo $(grep "^e2fsprogs" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     libcap) echo $(grep "^libcap" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     util-linux-libs | util-linux-pass1) echo $(grep "^util-linux" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     xz-utils) echo $(grep "^xz" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     sqlite) echo $(grep "^sqlite" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     udev) echo $(grep "^systemd" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     xml-parser) echo $(grep "^XML" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     libdbus) echo $(grep "^dbus" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
     *)  echo $(grep "^$script_name[_-][[:digit:]]" $JHALFSDIR/pkg_tarball_list | head -n1 )
         ;;
   esac

}

#----------------------------------#
get_package_version() {            #
#----------------------------------#
# Those hacks may change depending on the invention of devs for tarball names.
# Normally, we expect -digit to signal the beginning of the version field
# but it may happen that there is no '-' (tcl, expect), that the version
# field is ddd-d (pkg-config-lite-028-1, since sed is greedy, it finds only
# the second dash) or that the first character of the version is not a digit
# (iputils-s20121221)...
  case ${1} in
     tcl*|expect*|tzdata*|xmlts*|unzip*|lynx*)
                           echo ${1} | sed -e 's@^[^[:digit:]]*@@' \
                                           -e 's@\(-src\)*\.tar.*@@'
        ;;
     iputils*)             echo ${1} | sed -e 's@^.*-@@' \
                                           -e 's@\.tar.*@@'
        ;;
     LVM*)                 echo ${1} | sed -e 's@^[^.]*\.@@' \
                                           -e 's@\.tgz.*@@'
        ;;
     iproute*)             echo ${1} | sed -e 's@^[^-]*-@@' \
                                           -e 's@\.tar.*@@'
        ;;
     pkg*|udev*|Singular*|XML*)
                           echo ${1} | sed -e 's@^.*[a-z]-\([[:digit:]]\)@\1@' \
                                           -e 's@\.tar.*@@' \
                                           -e 's@\.zip.*@@'
        ;;
     *)                    echo ${1} | sed -e 's@^.*[-_]\([[:digit:]]\)@\1@' \
                                           -e 's@\.tar.*@@' \
                                           -e 's@\.zip.*@@'
        ;;
  esac

}
#=======================


#==== TARGET HEADER ====

#----------------------------------#
LUSER_wrt_target() {               # Create target and initialize log file
#----------------------------------#
  local i=$1
  local PREV=$2
  local version
  if [ "$3" != "" ]; then
    version=-$3
  fi
(
cat << EOF

$i:  $PREV
	@\$(call echo_message, Building)
${NO_PROGRESS}	@export BASHBIN=\$(SHELL) && \$(SHELL) progress_bar.sh \$@ \$\$PPID &
	@echo "\$(nl_)\`date\`\$(nl_)" >logs/\$@$version
	@\$(PRT_DU) >>logs/\$@$version
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_wrt_target() {              # Create target and initialize log file
#----------------------------------#
  local i=$1
  local PREV=$2
  local version
  if [ "$3" != "" ]; then
    version=-$3
  fi
(
cat << EOF

$i:  $PREV
	@\$(call echo_message, Building)
${NO_PROGRESS}	@export BASHBIN=\$(SHELL) && \$(SHELL) progress_bar.sh \$@ \$\$PPID &
	@echo "\$(nl_)\`date\`\$(nl_)" >logs/\$@$version
	@\$(PRT_DU_CR) >>logs/\$@$version
EOF
) >> $MKFILE.tmp
}

#=======================


#======== UNPACK =======

#----------------------------------#
LUSER_wrt_unpack() {               # Unpack and set 'ROOT' var
#----------------------------------#
  local FILE=$1
  local optSAVE_PREVIOUS=$2

  if [[ "${optSAVE_PREVIOUS}" != "1" ]]; then
(
cat << EOF
	@\$(call remove_existing_dirs,$FILE)
EOF
) >> $MKFILE.tmp
  fi

(
cat << EOF
	@\$(call unpack,$FILE)
	@\$(call get_pkg_root_LUSER)
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_Unpack() {                  #
#----------------------------------#
  local FILE=$1
  local optSAVE_PREVIOUS=$2

  if [ "${optSAVE_PREVIOUS}" != "1" ]; then
(
cat << EOF
	@\$(call remove_existing_dirs2,$FILE)
EOF
) >> $MKFILE.tmp
  fi
(
cat  << EOF
	@\$(call unpack2,$FILE)
	@\$(call get_pkg_root2)
EOF
) >> $MKFILE.tmp
}

#=======================


#===== TESTS LOGS ======

#----------------------------------#
LUSER_wrt_test_log() {             # Initialize testsuite log file
#----------------------------------#
  local version
  if [ "$2" != "" ]; then
    version=-$2
  fi
  local TESTLOGFILE=$1$version
(
cat  << EOF
	@echo "export  TEST_LOG=\$(TESTLOGDIR)/$TESTLOGFILE" >> envars && \\
	echo "\$(nl_)\`date\`\$(nl_)" >\$(TESTLOGDIR)/$TESTLOGFILE
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_wrt_test_log() {            #
#----------------------------------#
  local version
  if [ "$2" != "" ]; then
    version=-$2
  fi
  local TESTLOGFILE=$1$version
(
cat  << EOF
	@echo "export TEST_LOG=\$(crTESTLOGDIR)/$TESTLOGFILE" >> envars && \\
	echo "\$(nl_)\`date\`\$(nl_)" >\$(crTESTLOGDIR)/$TESTLOGFILE
EOF
) >> $MKFILE.tmp
}

#=======================


#======== RUN AS =======

#----------------------------------#
wrt_RunAsRoot() {                  # Some scripts must be run as root..
#----------------------------------#
  local MOUNT_ENV
  local version
  if [ "$2" != "" ]; then
    version=-$2
  fi
  local file=$1

  case ${PROGNAME} in
    lfs   ) MOUNT_ENV="LFS" ;;
    clfs  ) MOUNT_ENV="CLFS" ;;
    clfs2 ) MOUNT_ENV="CLFS" ;;
    clfs3 ) MOUNT_ENV="CLFS" ;;
    hlfs  ) MOUNT_ENV="HLFS" ;;
    *) echo "undefined progname $PROGNAME"; exit 1 ;;
  esac

(
cat << EOF
	@export ${MOUNT_ENV}=\$(MOUNT_PT) && \\
	${PROGNAME}-commands/`dirname $file`/\$@ >>logs/\$@$version 2>&1 && \\
	\$(PRT_DU) >>logs/\$@$version
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
LUSER_wrt_RunAsUser() {            # Calculate time with perl, footer to log file
#----------------------------------#
  local file=$1
  local version
  if [ "$2" != "" ]; then
    version=-$2
  fi

(
cat << EOF
	@source ~/.bashrc && \\
	\$(CMDSDIR)/`dirname $file`/\$@ >> logs/\$@$version 2>&1 && \\
	\$(PRT_DU) >>logs/\$@$version
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_wrt_RunAsRoot() {           #
#----------------------------------#
  local file=$1
  local version
  if [ "$2" != "" ]; then
    version=-$2
  fi
(
cat << EOF
	@source envars && \\
	\$(crCMDSDIR)/`dirname $file`/\$@ >>logs/\$@$version 2>&1 && \\
	\$(PRT_DU_CR) >>logs/\$@$version
EOF
) >> $MKFILE.tmp

}

#=======================


#====== COPY FSTAB =====

#----------------------------------#
LUSER_wrt_CopyFstab() {            #
#----------------------------------#
(
cat << EOF
	@cp -v \$(MOUNT_PT)/sources/fstab \$(MOUNT_PT)/etc/fstab >>logs/\$@ 2>&1 && \\
	\$(PRT_DU) >>logs/\$@
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_wrt_CopyFstab() {           #
#----------------------------------#
(
cat << EOF
	@cp -v /sources/fstab /etc/fstab >>logs/\$@ 2>&1 && \\
	\$(PRT_DU_CR) >>logs/\$@
EOF
) >> $MKFILE.tmp
}

#=======================


#==== INSTALLED FILES LOGS ====

#----------------------------------#
LUSER_wrt_TouchTimestamp() {       #
#----------------------------------#
(
cat << EOF
	@\$(call touch_timestamp_LUSER)
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_wrt_TouchTimestamp() {      #
#----------------------------------#
(
cat << EOF
	@\$(call touch_timestamp)
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
LUSER_wrt_LogNewFiles() {          #
#----------------------------------#
  local name=`echo ${1} | sed 's/[0-9]-//'`
(
cat << EOF
	@\$(call log_new_files_LUSER,$name)
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_wrt_LogNewFiles() {         #
#----------------------------------#
  local name=`echo ${1} | sed 's/[0-9]-//'`
(
cat << EOF
	@\$(call log_new_files,$name)
EOF
) >> $MKFILE.tmp
}

#=======================


#==== RM BUILD DIRS ====

#----------------------------------#
LUSER_RemoveBuildDirs() {          #
#----------------------------------#
  local name=`echo ${1} | sed 's/[0-9]-//'`
(
cat << EOF
	@\$(call remove_build_dirs,$name)
EOF
) >> $MKFILE.tmp
}

#----------------------------------#
CHROOT_wrt_RemoveBuildDirs() {     #
#----------------------------------#
  local name=`echo ${1} | sed 's/[0-9]-//'`
(
cat << EOF
	@\$(call remove_build_dirs2,$name)
EOF
) >> $MKFILE.tmp
}

#=======================


#======== TOUCH ========

#----------------------------------#
wrt_touch() {                      #
#----------------------------------#
(
cat << EOF
	@\$(call housekeeping)
EOF
) >> $MKFILE.tmp
}

#=======================
